﻿<%
class BlogItemClass
    public property get CACHE_PREFIX 
        CACHE_PREFIX = "BLOGITEM"
    end property
        
    public blogId, itemId, title, userName, email, webUrl, face, isRSS
    public detail,more, excerpt, startDate, endDate, insertTime,updateTime
    public userId,  isSecret, replyCount, trackCount, showContent
    public catId, catName
    public m_comments, m_tracks
        
    private sub class_initialize()
       isRSS = False
    end sub
    
    private sub class_terminate()
    
    end sub
        
    private function build(array)
        build = false
        if isEmpty(array) or not isArray(array) then
            exit function
        end if
        if ubound(array)<24 then
            exit function
        end if
        
        blogId      = array(0)
        itemId      = array(1)  
        title       = array(2)  
        userName    = array(3)  
        email       = array(4)  
        webUrl      = array(5)  
        face        = array(6)  
        detail      = array(7)  
        more        = array(8)
        excerpt     = array(9 )   
        startDate   = array(10)  
        endDate     = array(11)  
        insertTime  = array(12)  
        updateTime  = array(13)  
        userId      = array(14)  
        isSecret    = array(15) 
        replyCount  = array(16) 
        trackCount  = array(17) 
        showContent = array(18) 
        catID       = array(19) 
        catName     = array(20) 
        m_comments  = array(21) 
        m_tracks    = array(22) 
        isRSS				= array(23) 
        
         
        build = true
    end function
    
    private function toArray()
        dim array(24)
        array(0) = blogId
        array(1) = itemId
        array(2) = title 
        array(3) = userName 
        array(4) = email
        array(5) = webUrl
        array(6) = face
        array(7 ) = detail    
        array(8 ) = more
        array(9 ) = excerpt
        array(10) = startDate 
        array(11) = endDate 
        array(12) = insertTime 
        array(13) = updateTime
        array(14) = userId 
        array(15) = isSecret 
        array(16) = replyCount 
        array(17) = trackCount 
        array(18) = showContent 
        array(19) = catId 
        array(20) = catName
        array(21) = m_comments  
        array(22) = m_tracks    
        array(23) = isRSS
                
        toArray = array
    end function
    

    public function load(item_Id)
        load = false
        if isEmpty(item_Id) then
            exit function
        end if
        
        itemId= clng(item_Id)
        
        load = loadFromCache
        if load=false then
            load = loadFromDB()
            if (load=true) then
                call cache()
            end if    
        end if    
    end function
    
    public function reload(item_Id)
        reload = false
        if isEmpty(item_Id) then
            exit function
        end if
        
        itemId= clng(item_Id)

        reload = loadFromDB()
        if reload=true then
            m_comments = Empty
            m_tracks = Empty
            call cache()
        else
            call clean()
        end if    
        

    end function
    
    public function clean()
        call removeFromCache(CACHE_PREFIX, clng(itemId))
        blogId      = Empty
        itemId      = Empty
        title       = Empty
        userName    = Empty
        email       = Empty
        webUrl      = Empty
        face        = Empty
        detail      = Empty
        more        = Empty
        excerpt     = Empty
        startDate   = Empty
        endDate     = Empty
        insertTime  = Empty      
        updateTime  = Empty
        userId      = Empty
        isSecret    = Empty
        replyCount  = Empty
        trackCount  = Empty
        showContent = Empty
        catID       = Empty
        catName     = Empty
        m_comments  = Empty
        m_tracks    = Empty  
        isRSS				= False
    end function
    
    private function loadFromCache()
        loadFromCache = build(getFromCache(CACHE_PREFIX, clng(itemId)))
    end function
    
    public sub cache()
       if not isEmpty(itemId) then
           call build(putIntoCache(CACHE_PREFIX, clng(itemId), toArray()))
       end if
    end sub
    

    private function loadFromDB()
        loadFromDB = false
        
        if isRSS then
        	exit function
        end if

        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
        call openDB
        'response.write "loadFromDB"

        
        sql = " select a.blog_id, a.content_id,  a.title, a.detail,a.more, a.excerpt, a.start_date, a.end_date, a.insert_time,a.update_time, a.user_id, a.user_name, a.email, a.web_url, a.face, a.is_secret, a.reply_count, a.track_count, a.cat_id, a.show_content, b.cat_name from blog_content a left join blog_category b on a.cat_id=b.cat_id where a.content_id=" & itemId 
        rs.open sql,conn,1,1
        if not rs.eof then
            blogId      = rs("blog_id")
            itemId      = rs("content_id")
            title       = rs("title")
            detail      = rs("detail")
            more        = rs("more")
            excerpt     = rs("excerpt")
            startDate   = rs("start_date")
            endDate     = rs("end_date")
            insertTime  = rs("insert_time")
            updateTime  =  rs("update_time")
            userId      = rs("user_id")
            userName    = rs("user_name")
            email       = rs("email")
            webUrl      = rs("web_url")
            face        = rs("face")
            isSecret    = rs("is_secret")
            showContent = rs("show_content")
            replyCount  = rs("reply_count")
            trackCount  = rs("track_count")
            catId       = rs("cat_id")
            catName     = rs("cat_name")
        
            loadFromDB = true        
        end if                
        rs.close      
        set rs = nothing
    end function
    
    
    
    public function loadCommentsFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
        call openDB
        sql = " select a.title,a.is_secret as content_secret,b.blog_id, b.content_id,b.parent_id, b.comment_id, b.detail, b.insert_time,b.update_time, b.user_id, b.user_name, b.is_secret, b.email, b.web_url, b.face from blog_content a, blog_comments b  where a.content_id=b.content_id and  b.content_id=" & itemId & " order by b.insert_time "
        rs.open sql,conn,1,1

        dim count, blogComment, i
        count = rs.recordCount
        redim m_comments(count)
        if count>0 then
            for i=0 to count -1    
                set blogComment = new BlogCommentClass
                blogComment.buildFromRS(rs)
                m_comments(i) = blogComment.toArray()
                rs.moveNext()
            next
        end if    
        rs.close()  
        set rs = nothing
    end function
    
        
    public function getComments
        if isEmpty(m_comments) then
            call loadCommentsFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_comments)
        if count<1 then
            exit function
        end if 
        
        dim blogComments, blogComment, i
        redim blogComments(count)
        for i=0 to count-1 
            set blogComment = new blogCommentClass
            call blogComment.build(m_comments(i))
            set blogComments(i) = blogComment
        next
        
        getComments = blogComments
    end function
        
        

    
    public function loadTracksFromDB()
        dim sql, rs
        set rs = Server.CreateObject("ADODB.Recordset")
        call openDB
        sql = " select b.blog_id, b.content_id, track_id, url, title, excerpt, user_name, user_id, host_name, host_url, insert_time from blog_track b  where b.content_id=" & itemId & " order by b.insert_time "
        
        rs.open sql,conn,1,1

        dim count, blogTrack
        count = rs.recordCount
        redim m_tracks(count)
        if count>0 then
            for i=0 to count -1    
                set blogTrack = new BlogTrackClass
                blogTrack.buildFromRS(rs)   
                m_tracks(i) = blogTrack.toArray()
                rs.moveNext()
            next
        end if    
        rs.close() 
        set rs = nothing
    end function
    
        
    public function getTracks
        if isEmpty(m_tracks) then
            call loadTracksFromDB()
            call cache()
        end if    
        
        dim count
        count = ubound(m_tracks)
        if count<1 then
            exit function
        end if 
        
        dim blogTracks, blogTrack, i
        redim blogTracks(count)
        for i=0 to count-1 
            set blogTrack = new BlogTrackClass
            call blogTrack.build(m_tracks(i))
            set blogTracks(i) = blogTrack
        next
        
        getTracks = blogTracks
    end function
            
end class

%>